package com.wanshi.controller;

import com.wanshi.po.Record;
import com.wanshi.po.Result;
import com.wanshi.service.RecordService;
import com.wanshi.service.TiService;
import com.wanshi.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

//张万鹏测试git33
@SuppressWarnings("all")
@Controller
@RequestMapping("dati")
public class Dati2Controller {

    @Autowired
    private TiService tiService;

    @Autowired
    private RecordService recordService;


    @RequestMapping(value = "/test78")
    @ResponseBody
    public Result test78(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);
        DaTiUtils<Integer> daTiUtils = new DaTiUtils<>();
        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            int n = 3;

            int result = daTiUtils.writeContent(uuid, content, method, new Object[]{3});
            flag = flag && result == 9;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }
            n = 4;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && result == 33;
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    @RequestMapping(value = "/test79")
    @ResponseBody
    public Result test79(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);
        DaTiUtils<String> daTiUtils = new DaTiUtils<>();
        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            int n = 34;

            String result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && "43".equals(result);
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }
            n = 23456;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && "65432".equals(result);
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    @RequestMapping(value = "/test81")
    @ResponseBody
    public Result test81(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);
        DaTiUtils<int[]> daTiUtils = new DaTiUtils<>();
        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            int[] n = new int[]{6, 2, 5, 8, 3, 6, 9, 4};
            int[] result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && Arrays.equals(result, new int[]{9, 4, 5, 8, 3, 6, 6, 2});
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    private String twoAdd(int n) {
        String ss = "";
        for (int i = 2; i <= n / 2 + 1; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                ss += n + "=" + (i) + "+" + (n - i);
                break;
            }
        }
        return ss;
    }

    public static boolean isPrime(int num) {
        if (num <= 3) {
            return num > 1;
        }
        // 不在6的倍数两侧的一定不是质数
        if (num % 6 != 1 && num % 6 != 5) {
            return false;
        }
        int sqrt = (int) Math.sqrt(num);
        for (int i = 5; i <= sqrt; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    @RequestMapping(value = "/test82")
    @ResponseBody
    public Result test82(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);
        DaTiUtils<String> daTiUtils = new DaTiUtils<>();
        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            int n = 6;

            String result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && "6=3+3".equals(result);
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }
            n = 8;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && "8=3+5".equals(result);
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }
            n = 10000;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{n});
            flag = flag && "10000=59+9941".equals(result);
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    @RequestMapping(value = "/test84")
    @ResponseBody
    public Result test84(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);
        DaTiUtils<String> daTiUtils = new DaTiUtils<>();
        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            if (content.contains("toUpperCase")) {
                flag = false;
                return RequestUtils.getResult(recordService, record, flag);
            }
            String s1 = "bCdeFG";

            String result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1});
            flag = flag && "BCDEFG".equals(result);

        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    //System.out.println(aaa(5500));//0.0
    //System.out.println(aaa(5600));//1.2
    //System.out.println(aaa(90000));//19440.0
    //System.out.println(aaa(40000));//5090.0
    @RequestMapping(value = "/test85")
    @ResponseBody
    public Result test85(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);

        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DaTiUtils<Double> daTiUtils = new DaTiUtils<>();
        try {
            int s1 = 5500;
            double result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1});
            flag = flag && result==0.0;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 5600;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1});
            flag = flag && result==1.2;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 90000;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1});
            flag = flag && result==19440.0;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 40000;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1});
            flag = flag && result==5090.0;
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    @RequestMapping(value = "/test86")
    @ResponseBody
    public Result test86(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);

        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DaTiUtils<Double> daTiUtils = new DaTiUtils<>();
        try {
            int s1 = 2;
            int s2 = 3;
            String type = "+";
            double result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2,type});
            flag = flag && result==5.0;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 4;
            s2 = 1;
            type = "-";
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2,type});
            flag = flag && result==3.0;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 4;
            s2 = 3;
            type = "*";
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2,type});
            flag = flag && result==12.0;
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = 10;
            s2 = 4;
            type = "/";
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2,type});
            flag = flag && result==2.5;
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    public static String t87(int year){
        int num = (2022 - year)%12;
        switch (num){
            case 0:
                return "虎";
            case 1:
                return "牛";
            case 2:
                return "鼠";
            case 3:
                return "猪";
            case 4:
                return "狗";
            case 5:
                return "鸡";
            case 6:
                return "猴";
            case 7:
                return "羊";
            case 8:
                return "马";
            case 9:
                return "蛇";
            case 10:
                return "龙";
            case 11:
                return "兔";
        }
        return "";
    }

    @RequestMapping(value = "/test87")
    @ResponseBody
    public Result test87(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);

        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DaTiUtils<String> daTiUtils = new DaTiUtils<>();
        try {
            Random random = new Random();
            for(int i = 1;i<=5;i++){
                int year =  random.nextInt(2022)+1;
                String result = daTiUtils.writeContent(uuid, content, method, new Object[]{year});
                if(!t87(year).equals(result)){
                    flag =  false;
                    break;
                }
            }
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    public static int[] t88(int[] arr,int num){
        //1.定义长度为10的原数组
        //2.定义长度为11的空数组
        int[] brr = new int[arr.length+1];
        int index = 0;
        //5.循环遍历原数组
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] <= num) {
                //5.1如果元素小于等于要插入的数字，则直接存放
                brr[i] = arr[i];
                //5.2把i后面的位置记录下来
                index = i + 1;
            }else{
                //5.3如果元素大于要插入的数字,则往后一个位置存放
                brr[i+1] = arr[i];
            }
        }
        //6.index存储的就是要插入的位置
        brr[index] = num;
        return brr;
    }

    @RequestMapping(value = "/test88")
    @ResponseBody
    public Result test88(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);

        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DaTiUtils<int[]> daTiUtils = new DaTiUtils<>();
        try {
            int[] s1 = {12,16,45,66,68,70};
            int s2 = 50;
            int[] result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2});
            flag = flag && Arrays.equals(result,t88(s1,s2));
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            s1 = new int[]{1,6,45,66,68,70};
            s2 = 67;
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{s1,s2});
            flag = flag && Arrays.equals(result,t88(s1,s2));
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }

    public static int[] t89(int[] arr){
        int[] brr = new int[arr.length];
        int left = 0;
        //4.定义变量代表偶数要存放的位置
        int right = arr.length-1;
        //5.对原数组进行遍历
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 == 1){
                //5.1如果是奇数就往左边放
                brr[left] = arr[i];
                //5.2存放之后让索引向右移动
                left++;
            }else{
                //5.3如果是偶数就往右边放
                brr[right] = arr[i];
                //5.4存放之后让索引向左移动
                right--;
            }
        }
        return brr;
    }

    @RequestMapping(value = "/test89")
    @ResponseBody
    public Result test89(HttpSession session, int id, String content, String method) {
        if (TextUtil.kong(content)) return new Result(Constants.ERROR);
        Record record = RequestUtils.getRecord(session, id, content);

        boolean flag = true;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DaTiUtils<int[]> daTiUtils = new DaTiUtils<>();
        try {
            //int[] s1 = {12,16,45,66,68,70};

            int[] tempArr1 = new int[]{12,16,45,66,68,70};
            int[] tempArr2 = new int[]{12,16,45,66,68,70};

            int[] result = daTiUtils.writeContent(uuid, content, method, new Object[]{tempArr1});
            flag = flag && Arrays.equals(result,t89(tempArr2));
            if (!flag) {
                return RequestUtils.getResult(recordService, record, flag);
            }

            tempArr1 = new int[]{1,6,45,66,68,70};
            tempArr2 = new int[]{1,6,45,66,68,70};
            result = daTiUtils.writeContent(uuid, content, method, new Object[]{tempArr1});
            flag = flag && Arrays.equals(result,t89(tempArr2));
        } catch (Exception e) {
            flag = false;
        } finally {
            ThreadPoolUtils.pool.execute(new AsynOprDatabaseUtil(recordService, record, flag,uuid));
            return RequestUtils.getResult(recordService, record, flag);
        }
    }
}
